<?php
/**
 * Created by PhpStorm.
 * User: stupboy
 * Date: 2018/3/18
 * Time: 1:08
 * Mail:87658989@qq.com
 */

namespace app\cron\controller;

use app\api\model\Driver;
use app\base\service\Redis;

class Track
{
    public $appPrefix;
    /**
     * 每5秒写入数据库，更新司机定位数据
     * @throws \Exception
     * @throws \app\base\exception\CacheException
     */
    public function updateDriverLocation(){
        $newRedis = new Redis();
        $redis          = $newRedis->redis;
        // $redisConfig    = Config('database.redis');
        // $this->appPrefix= $redisConfig['prefix'];
        // $redis->connect($redisConfig['host'],$redisConfig['port']);
        $arr            = [];
        while($data = $redis->rpop('car.track.4')){
            $driver= unserialize($data);
            if(empty($driver)){
                continue;
            }
            $temp['driver_id'] = $driver['driver_id'];
            $temp['lon'] = $driver['lon'];
            $temp['lat'] = $driver['lat'];
            $temp['update_time'] = date('Y-m-d H:i:s',$driver['gps_time']/1000);
            $arr[] = $temp;
            if(count($arr)>50) break;
        }
        $driver = new Driver();
        $driver->saveAll($arr);
    }
}